|
Contents |
|
$ cd MY_PROJECT_DIR $ rails new my_app -d postgresql $ cd my_app
$ git init $ git add . $ git commit -v
$ git clone git://jjedgar.git.sourceforge.net/gitroot/jjedgar/edgar2
and run:gem 'jquery-ui-rails' gem 'edgar', path: 'edgar2' gem 'rails-i18n' gem 'will_paginate', '~> 3.0.0' gem 'remotipart', '~> 1.0.2' gem 'bcrypt-ruby', require: "bcrypt" gem 'acts_as_tree', '~> 1.1.0' group :development do gem 'yard' gem 'rails-erd' end
$ bundle install
$ $EDITOR config/database.yml # config database
$ rake edgar_engine:install:migrations
$ rake db:create $ rake db:migrate $ rake db:setup
include Edgar::ControllerMixinForApp include Edgar::AuthenticationMixin before_filter :require_login
include EdgarHelper
//= require jquery.ui.dialog //= require jquery.ui.datepicker //= require jquery.remotipart //= require edgar/base //= require edgar/menu //= require edgar/operator_selection
*= require jquery.ui.dialog *= require jquery.ui.datepicker *= require edgar/base *= require edgar/menu
(NOTE: execution 'rake edgar_engine:install:migrations' above already generates session table so that no migration here is necessary)MyApp::Application.config.session_store :active_record_store
And access http://localhost:3000 by your browser. If you can see Rails logo, that's fine for now. Otherwise, something is wrong so that go back to the previous step and check.$ rails server
Let me assume here to develop Product and Order models, controllers, and views. Where, Product:Order = 1:many relations.
$ rails generate edgar:scaffold Product name:string price:integer $ rake db:migrate
$ cp edgar2/test/dummy/app/views/layouts/application.html.erb app/views/layouts/
$ cp edgar2/test/dummy/app/views/layouts/login.html.erb app/views/layouts/
edit config/edgar/menu_config.rb to add 'products' entry like the followings:$ mkdir config/edgar $ cp edgar2/test/dummy/config/edgar/menu_config.rb config/edgar/
('authors' and 'books' are for just Edgar engine's dummy app. Please delete them.)module Edgar::MenuConfig def top ... 'products', '_separator', ... end
$ cp edgar2/db/seeds.rb db/ $ rake db:seed
$ cd edgar2 $ tar cvf /tmp/x.tar test/fixtures/edgar* $ cd - # go back to previous (should be Rails root) $ tar xvf /tmp/x.tar # copy edgar2/test/fixtures/edgar* to test/fixtures/edgar* $ rake db:fixtures:load # load test/fixtures/ to development DB
$ rails generate edgar:scaffold Order name:string product_id:integer quantity:integer $ rake db:migrate
module Edgar::MenuConfig def top ... 'products', 'orders', '_separator', ... end
Edgar supports the following levels of customization (from the easiest level to the highest level):
Use Rails 'migration' to add any number of columns (actually limited by DBMS). The added columns are automatically displayed at list, form, search-form, and popup-list as default behavior. Of course, it is controllable which column is displayed or not. See 'Add model' section later.
Following example shows how to change Customer model's name column to first_name and last_name:
$ rails generate migration change_name_of_customers $ vi db/migrate/YYYYMMDDNNNNNN_change_name_of_customers.rb # YYYYMMDDNNNNNN is just sample, specify actual file name
class ChangeNameOfCustomers < ActiveRecord::Migration def up remove_column :customers, :name add_column :customers, :first_name, :string add_column :customers, :last_name, :string end def down remove_column :products, :first_name remove_column :products, :last_name add_column :products, :name, :string end end
$ rake db:migrate
NOTE: Because first_name and last_name have nil just after executing this migration, draw '?' when it is nil. When both columns are required field, this method can simply be first_name + ' ' + last_name.class Customer < ActiveRecord::Base : validates_presence_of :first_name, :last_name : def name (first_name || '?') + ' ' + (last_name || '?') end end
If you feel current Edgar web design is not good (maybe yes...), you can change layout(app/views/layout/*) and/or stylesheet(app/assets/stylesheets/*).
Model, view, and controller, which are generated by edgar:scaffold, can be customized as you want.
This section shows the easiest step, to select displayed columns. Please refer later sections for further customization (e.g. put calculated field, customize view, ...).
You see all of columns at list, form, search-form, and popup (when the model is referred by 'belongs_to') just after executing edgar:scaffold(*) Some columns are not necessary to display for the app. The DB defined column order may be not suitable. Edgar provides easy way to fix this level of customize at the sections below.
(*) Precisely to say, form doesn't show id, created_at, and updated_at.
You can overwrite class method 'view_columns()', which should return array of string, to specify which columns to be displayed in which order for all of the model's partial-templates (list, form, search-form, and popup-list). Example:
class Product < ActiveRecord::Base : def self.view_columns %w(id name) end : end
4 partial-templates, which are list, form, search-form, and popup-list, are controlled by EdgarController and EdgarPopupController. When you would like to define columns to be displayed and its order for each partial-template, overwrite class methods as follows:
Each method should return string array as the same as view_columns(). User model shows the actual example as:
class User < ActiveRecord::Base : # overwrite AR::Base.view_list_columns() def self.view_list_columns %w(id login email created_at updated_at) end : end
At ActiveRecord::Base, view_X_columns(), where X is list, form, ..., defines to call view_columns() as the default. So, total 5 methods are used as following priority:
After customized, please confirm if the program has no bug by:
$ rake test
When you would like to change screen layout and/or display calculated field, and so on a model, it is not enough to customize at previous section.
This section explains doing so by customize view without any change at controller which inherits from EdgarController.
Edgar:scaffold doesn't generate views because it refers EdgarController's views at default in Edgar plugin as follows (Thank you for Rails3's great feature!):
edgar/_form.html.erb: | form |
edgar/_list.html.erb: | list |
edgar/_search_form.html.erb: | search form |
edgar/index.html.erb: | page |
These can be overwritten at each view directory.
For example, product model specific form view can be set at app/views/products/_form.html.erb, which is automatically used prior to default edgar/_form.html.erb (again, that's Rails-3 feature). The app/views/products/_form.html would be as follows:
... <%= edgar_form do |f| %> <%= draw_form_buttons %> <table> <tr><th>Name</th> <td><%= f.text_field :name %></td></tr> <tr><th>Price</th> <td><%= f.text_field :price %></td></tr> </table> <% end %> ...
When it is not enough to customize view tempmlate, it's now the time to add action for the controller. It is usual activity to develop rails controller action.
When the controller doesn't use almost all of EdgarController providing features, it might be good to inherit ActionController::Base and write it own methods. This is normal rails application development rather than Edgar customization.
edgar:scaffold generates functional and unit test. The unit test is the same as plain scaffold, but the functional test is heavily customized and requires Edgar specific test helper and fixtures so that edit test/test_helper.rb as follows:
group :test do gem 'shoulda-context' end
require 'shoulda-context' Dir[Rails.root + 'edgar2/test/support/**/*.rb'].each { |f| require f }
class ActiveSupport::TestCase include Edgar::ControllerSupporter ...
$ cd edgar2 $ tar cvf /tmp/x.tar test/fixtures/edgar* $ cd - # go back to previous (should be Rails root) $ tar xvf /tmp/x.tar # copy edgar2/test/fixtures/edgar/ to test/fixtures/edgar/
set_fixture_class edgar_view_status: 'Edgar::ViewStatus'
ENV["RAILS_ENV"] = "test" require File.expand_path('../../config/environment', __FILE__) require 'rails/test_help' require 'shoulda-context' Dir[Rails.root + 'edgar2/test/support/**/*.rb'].each { |f| require f } class ActiveSupport::TestCase include Edgar::ControllerSupporter fixtures :all set_fixture_class edgar_view_status: 'Edgar::ViewStatus' end
$ rake test